﻿#include "ICDManagement.h"
#include "ui_ICDManagement.h"
#include <QDebug>

ICDManagement::ICDManagement(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ICDManagement)
{
    ui->setupUi(this);

    ui->icdTree->setColumnWidth(0,185);
    ui->icdTree->setColumnWidth(1,95);
    ui->icdTree->setColumnWidth(2,60);
    setPlaneTypeIDNameData();
    SetIcon();
    analogData();
    loadData();
}

ICDManagement::~ICDManagement()
{
    delete ui;
}

///
/// \brief ICDManagement::setPlaneTypeIDNameData
///初始化：机型ID和名称信息
void ICDManagement::setPlaneTypeIDNameData()
{
    m_PlaneTypeIDNameMap.insert(1001,"xx单座");
    m_PlaneTypeIDNameMap.insert(1002,"xx双座");
    m_PlaneTypeIDNameMap.insert(1003,"XXA");
    m_PlaneTypeIDNameMap.insert(1004,"XXB");
    m_PlaneTypeIDNameMap.insert(1005,"XXC");
    m_PlaneTypeIDNameMap.insert(10050001,"FC");
    m_PlaneTypeIDNameMap.insert(10050002,"1553B");
}

///
/// \brief ICDManagement::analogData
///模拟授权模块数据
void ICDManagement::analogData()
{
    m_ModuleInfoVec.append(1001);
    m_ModuleInfoVec.append(1002);
    m_ModuleInfoVec.append(1003);
    m_ModuleInfoVec.append(1004);
    m_ModuleInfoVec.append(1005);
    m_ModuleInfoVec.append(10050001);
    m_ModuleInfoVec.append(10050002);
}

///
/// \brief ICDManagement::getPlaneTypeID
///获取授权机型id
void ICDManagement::getPlaneTypeID(QString planeTypeID)
{
    m_PlaneTypeIDStr =planeTypeID;
}

///
/// \brief ICDManagement::SetIcon
///设置图标
void ICDManagement::SetIcon()
{
    m_planeIcon.addPixmap(QPixmap(":/Resources/res/icd-plane.png"));
    m_fileIcon.addPixmap(QPixmap(":/Resources/res/icd-file.png"));
    m_folderIcon.addPixmap(QPixmap(":/Resources/res/icd-folder.png"));
    m_infoIcon.addPixmap(QPixmap(":/Resources/res/icd-info.png"));
}

///
/// \brief ICDManagement::LoadData
///加载ICD信息
void ICDManagement::loadData()
{
    QVector<ICD_INFO>::iterator icdIter;
    QVector<int>::iterator intIter;

    m_ICDInfoVec = SQLManagement::Instance()->QueryAllICDInfoData();

    if(m_ICDInfoVec.count()>0){  //如果读取的有数据，进行后续操作
        //遍历数据库内的ICD信息
        for (icdIter=m_ICDInfoVec.begin();icdIter!=m_ICDInfoVec.end();icdIter++) {
            ICD_INFO icdinfo = *icdIter;

            //遍历ICD信息，查看该机型模块是否被授权
            for (intIter = m_ModuleInfoVec.begin(); intIter != m_ModuleInfoVec.end(); intIter++) {
                if(icdinfo.icd_plane_type_id == *intIter){  //如果机型ID被授权，则执行建立节点前的判断
                    if(!m_BuildNodeMap.contains(icdinfo.icd_plane_type_id)){  //如果没有建立机型节点，则先建立机型节点
                        //建立机型节点
                        if(icdinfo.icd_plane_type_id<9999){  //建立一级节点
                            QString strTypeName = m_PlaneTypeIDNameMap[icdinfo.icd_plane_type_id];
                            QTreeWidgetItem* item = new QTreeWidgetItem(ui->icdTree);
                            item->setText(0,strTypeName);
                            item->setIcon(0,m_planeIcon);  //设置图标
                            m_PlaneTypeItemMap.insert(icdinfo.icd_plane_type_id,item);  //将机型id和item关联
                        }else{
                            if(!m_BuildNodeMap.contains(icdinfo.icd_plane_type_id/10000)){ //如果其父级节点没有建立，则建立其父级节点
                                QString fatherTypeName = m_PlaneTypeIDNameMap[icdinfo.icd_plane_type_id/10000]; //添加父节点
                                QTreeWidgetItem* fatherItem = new QTreeWidgetItem(ui->icdTree);
                                fatherItem->setText(0,fatherTypeName);
                                fatherItem->setIcon(0,m_planeIcon);  //设置图标
                                m_PlaneTypeItemMap.insert(icdinfo.icd_plane_type_id/10000,fatherItem);  //将机型id和item关联
                                m_BuildNodeMap.insert(icdinfo.icd_plane_type_id/10000,m_PlaneTypeIDNameMap[icdinfo.icd_plane_type_id/10000]); //其父节点建立后，登记到map内
                            }

                            QString childTypeName = m_PlaneTypeIDNameMap[icdinfo.icd_plane_type_id];  //添加子节点
                            QTreeWidgetItem* childItem = new QTreeWidgetItem(m_PlaneTypeItemMap[icdinfo.icd_plane_type_id/10000]);
                            childItem->setText(0,childTypeName);
                            childItem->setIcon(0,m_folderIcon); //设置图标
                            m_PlaneTypeItemMap.insert(icdinfo.icd_plane_type_id,childItem);  //将机型id和item关联
                        }
                        m_BuildNodeMap.insert(icdinfo.icd_plane_type_id,m_PlaneTypeIDNameMap[icdinfo.icd_plane_type_id]);  //该节点建立后，登记到map内
                    }
                    //建立icd信息节点
                    QString reviseTime = icdinfo.icd_revise_time;
                    QTreeWidgetItem* item = new QTreeWidgetItem(m_PlaneTypeItemMap[icdinfo.icd_plane_type_id]);
                    QPushButton* pButton=new QPushButton("方案");

                    item->setText(0,QString("ICD版本 ")+QString::number(icdinfo.icd_version));
                    item->setText(1,reviseTime);
                    item->setIcon(0,m_fileIcon); //设置图标
                    pButton->setMaximumWidth(50);

                    ui->icdTree->setItemWidget(item,2,pButton);
                    m_ICDidItemMap.insert(item,icdinfo.icd_id); //将该Item和id关联
                    m_ICDidButtonMap.insert(pButton,icdinfo.icd_id);  //将该Button和id关联

                    connect(pButton,&QPushButton::clicked,this,&ICDManagement::onItemButtonClicked); //按钮点击事件

                    break;
                }
            }
        }
    }
}

///
/// \brief ICDManagement::onItemButtonClicked
///处理点击事件
void ICDManagement::onItemButtonClicked()
{
    int icdID; //icd_id编号
    QPushButton* pButton = qobject_cast<QPushButton*>(sender());
    if(m_ICDidButtonMap.contains(pButton)){
        icdID = m_ICDidButtonMap[pButton];
    }
    //获取icd_id,并打开解析方案管理界面
    PublicData::setICDID(icdID);  //获取icd_id，提供给公共信息类
    emit openAnalysisPlanMGT(ANA_PLAN_MGT,ICD_MGT); //打开解析方案管理，关闭ICD管理
}

///
/// \brief ICDManagement::on_pbDelete_clicked
///删除ICD信息
void ICDManagement::on_pbDelete_clicked()
{
    //TODO获得界面选择的项，删除数据库内对应的数据
    QTreeWidgetItem* currentIem = ui->icdTree->currentItem();
    int icdID;  //icd信息编号

    if(!m_ICDidItemMap.contains(currentIem)){
        QMessageBox::information(this,"删除提示","请选择待删除的ICD信息");
    }else{
        icdID = m_ICDidItemMap[currentIem];  //当前选中项的icd信息编号
        if(SQLManagement::Instance()->DeleteICDInfoData(icdID)){
            m_ICDidItemMap.remove(currentIem);  //将map中该条记录移除
            delete currentIem->parent()->takeChild(ui->icdTree->currentIndex().row());  //删除该节点
        }
    }
}

///
/// \brief ICDManagement::on_pbExport_clicked
///导出icd信息
void ICDManagement::on_pbExport_clicked()
{
    //TODO获取界面选择的项，查询数据库内对应的数据，通过接口导出为指定的文件
    QTreeWidgetItem* currentIem = ui->icdTree->currentItem();
    ICD_INFO icdInfo;
    int icdID;  //icd信息编号

    if(!m_ICDidItemMap.contains(currentIem)){
        QMessageBox::information(this,"查询提示","数据库中没有该条数据");
    }else{
        icdID = m_ICDidItemMap[currentIem];  //当前选中项的icd信息编号
        icdInfo = SQLManagement::Instance()->QueryOneICDInfoData(icdID);
        qDebug()<<"icd_id: "<<icdInfo.icd_id;
    }
}

///
/// \brief ICDManagement::on_pbImport_clicked
///导入icd信息
void ICDManagement::on_pbImport_clicked()
{
    //先选择界面上所属的机型，再通过接口导入数据，写入数据库，并显示在界面对应的机型下
    QTreeWidgetItem* currentIem = ui->icdTree->currentItem();

    if(0 == m_PlaneTypeItemMap.key(currentIem))   //如果未选择机型
    {
        QMessageBox::information(this,"导入提示","请先选择所属机型");
    }
    else{
        importW.getPlaneTypeID(m_PlaneTypeItemMap.key(currentIem)); //将机型id传递过去
        importW.show();
    }
}
